/*
 * @Author: Drc
 * @Date: 2020-06-17 18:08:49
 * @LastEditors: Drc
 * @LastEditTime: 2021-09-24 10:31:50
 */
import {RootModel} from '@path/redux'
import {createModel} from '@rematch/core'
import {PickerOptions} from 'react-native-picker'

const utils = createModel<RootModel>()({
  state: {
    loading: {
      textContent: '',
      visible: false,
      cancelable: false,
      children: undefined,
    },
    toast: {
      text: '',
      duration: 1000,
      callback: () => {},
    },
    picker: {
      show: false,
      options: {} as PickerOptions,
    },
  },
  reducers: {
    showLoading(state: any, payload?: {textContent?: string; cancelable?: boolean; children?: any}) {
      return {
        ...state,
        loading: {
          ...state.loading,
          visible: true,
          ...payload,
        },
      }
    },
    hideLoading(state: any) {
      return {
        ...state,
        loading: {
          textContent: '',
          visible: false,
          cancelable: false,
          children: undefined,
        },
      }
    },
    showToast(state: any, payload: {text: string; duration?: number; callback?: Function}) {
      return {
        ...state,
        toast: {
          ...state.toast,
          text: typeof payload.text === 'string' ? payload.text : JSON.stringify(payload.text),
          duration: payload.duration ?? 1000,
          callback: payload.callback,
        },
      }
    },
    showPicker(state: any, options: PickerOptions) {
      return {
        ...state,
        picker: {
          ...state.picker,
          show: true,
          options,
        },
      }
    },
    hidePicker(state: any) {
      return {
        ...state,
        picker: {
          ...state.picker,
          show: false,
          options: {},
        },
      }
    },
  },
})

export default utils
